#include "../src/linklist.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

/* 声明底层链表的函数库 */
extern func_linklist fun_linklist;

typedef struct _tag_Teacher // 老师节点
{
    LinkListNode Header; // 链表逻辑节点
    int flag;            // 业务节点的类型标识
    int wages;           // 哈哈，不知道写什么信息，就写个工资吧
    char name[32];       // 姓名
} Teacher_t;

typedef struct _tag_Student // 学生节点
{
    LinkListNode Header; // 链表逻辑节点
    int flag;            // 业务节点的类型标识
    int age;             // 学生的年龄
    char name[32];       // 姓名
    struct score         //学生的成绩结构体
    {
        int english;
        int chinese;
        int math;
    } score;
} Student_t;

int main(int argc, char const **argv)
{
    int Length = 0, i = 0;
    Teacher_t t1, t2, t3, t4;
    Student_t s1, s2, s3;

    /* 初始化老师节点的信息 */
    t1.flag = 0; // 以 flag = 0 标识为老师节点
    t1.wages = 10;
    strcpy(t1.name, "张三");

    t2.flag = 0; // 以 flag = 0 标识为老师节点
    t2.wages = 11;
    strcpy(t2.name, "李四");

    t3.flag = 0; // 以 flag = 0 标识为老师节点
    t3.wages = 12;
    strcpy(t3.name, "王五");

    t4.flag = 0; // 以 flag = 0 标识为老师节点
    t4.wages = 13;
    strcpy(t4.name, "赵六");

    /* 初始化学生节点的信息 */
    s1.flag = 1; // 以 flag = 1 标识为老师节点
    s1.age = 20;
    strcpy(s1.name, "佩奇");
    s1.score.english = 40;
    s1.score.chinese = 41;
    s1.score.math = 42;

    s2.flag = 1; // 以 flag = 1 标识为老师节点
    s2.age = 21;
    strcpy(s2.name, "乔治");
    s2.score.english = 50;
    s2.score.chinese = 51;
    s2.score.math = 52;

    s3.flag = 1; // 以 flag = 1 标识为老师节点
    s3.age = 22;
    strcpy(s3.name, "社会人");
    s3.score.english = 60;
    s3.score.chinese = 61;
    s3.score.math = 62;

    /* 创建新的链表 */
    Linklist *list = fun_linklist.create();

    /* 插入老师节点和学生节点，头插入法 */
    fun_linklist.insert(list, (LinkListNode *)&(t1), 0); //老师节点
    fun_linklist.insert(list, (LinkListNode *)&(s1), 0); //学生节点
    fun_linklist.insert(list, (LinkListNode *)&(t2), 0); //老师节点
    fun_linklist.insert(list, (LinkListNode *)&(s2), 0); //学生节点
    fun_linklist.insert(list, (LinkListNode *)&(t3), 0); //老师节点
    fun_linklist.insert(list, (LinkListNode *)&(s3), 0); //学生节点
    fun_linklist.insert(list, (LinkListNode *)&(t4), 0); //老师节点

    /* 获取当前链表的长度 */
    Length = fun_linklist.length(list);
    printf("line = %3d, linklist length = %d\n", __LINE__, Length);

    putchar(10);

    for (i = 0; i < Length; i++)
    {
        /* 获取节点，此处采用笨办法，默认为老师节点，然后判断flag来最终确定节点类型 */
        Teacher_t *tlist = (Teacher_t *)fun_linklist.getNode(list, i);
        if (tlist != NULL)
        {
            if (tlist->flag == 0) // 为老师的节点
            {
                printf("line = %3d, Teacher, name = %8s, wages = %d\n",
                       __LINE__, tlist->name, tlist->wages);
            }
            else if (tlist->flag == 1) // 为学生节点
            {
                Student_t *temp = (Student_t *)tlist;
                printf("line = %3d, Student, name = %8s, age = %d, english = %3d, chinese = %3d, math = %3d\n",
                       __LINE__, temp->name, temp->age, temp->score.english,
                       temp->score.chinese, temp->score.math);
            }
        }
    }

    putchar(10);

    for (i = 0; i < Length; i++)
    {
        /* 删除节点，头删除法 */
        Teacher_t *tlist = (Teacher_t *)fun_linklist.delete(list, 0);

        if (tlist != NULL)
        {
            if (tlist->flag == 0) // 为老师的节点
            {
                printf("line = %3d, Teacher, name = %8s, wages = %d\n",
                       __LINE__, tlist->name, tlist->wages);
                /* 释放要删除的节点，并将临时指针指向NULL */
                // free(tlist);
                // tlist = NULL;
            }
            else if (tlist->flag == 1) // 为学生节点
            {
                Student_t *temp = (Student_t *)tlist;
                printf("line = %3d, Student, name = %8s, age = %d, english = %3d, chinese = %3d, math = %3d\n",
                       __LINE__, temp->name, temp->age, temp->score.english,
                       temp->score.chinese, temp->score.math);
                /* 释放要删除的节点，并将临时指针指向NULL */
                // free(temp);
                // temp = NULL;
            }
        }
    }

    /* 清空链表 */
    fun_linklist.clear(list);
    /* 重新获取链表的长度 */
    Length = fun_linklist.length(list);
    printf("line = %3d, linklist length = %d\n", __LINE__, Length);

    /* 销毁链表*/
    fun_linklist.destory(&list);

    if (list == NULL)
    {
        printf("list = NULL, free success\n");
    }
    else
    {
        printf("list = %p, free failed\n", list);
        free(list);
    }

    /* 创建新的链表 */
    Linklist *list1111 = fun_linklist.create();

    Teacher_t *tlist22222 = (Teacher_t *)fun_linklist.delete(list1111, 0);
    printf("tlist22222 = %p, tlist22222 failed\n", tlist22222);
    return 0;
}
